<?php

namespace App\Http\Controllers\web;


use App\Helpers\Image;
use App\Events\StoreProduct;
use App\Helpers\Audit;
use App\Helpers\Encryption;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreCategoryRequest;
use App\Helpers\Express;
use App\Repository\RepositoryInterface\ProductInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;


/**
 * 商品分类后台管理
 */
//商品类
class ProductController extends Controller
{
    //受保护模板
    protected $product;

    public function __construct(ProductInterface $product)
    {
        $this->product = $product;
    }


   
    /**
     * 订单物流信息跟踪
     * @return \Illuminate\Http\JsonResponse
     */
    public function express()
    {
        $com = 'zhongtong';//快递编码
        $num = '78284231994531';//订单编号
        $phone = '13151810965';//手机号
        $form = '长沙';//出发地
        $to = '上海';//目的地
        try {
            $result = Express::express($com, $num, $phone, $form, $to);
            return success('订单信息跟踪成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }

    }

    /**
     * 获取商品列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function goodsList(Request $request)
    {
        $keyword = $request->get('0');
        $where = [];
        if (!empty($keyword)) {
            $where[] = ['store_name', 'like', "$keyword%"];
        }
        try {
            $result = $this->product->goodsList($where)->toArray();
            foreach ($result as $val) {
                Redis::zincrby('rankList', $val['sales'], $val['id']);
            }
            return $this->success('数据获取成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }

    }

  /**
     * 商品排行榜
     * @return array
     */
    public function rankList()
    {
        try {
            $data = Redis::zRevRange('rankList', 0, 9);
            $ids = implode(',', $data);

            $score = Redis::zRevRange('rankList', 0, 9);
            $score = explode(',', implode(',', $score));
            $rankData = $this->product->goodsRank($data, $ids)->toArray();
            Redis::save();

            $arr = [];
            foreach ($rankData as $key => $val) {
                $arr[] = [
                    'id' => $val['id'],
                    'store_name' => $val['store_name'],
                    "image" => $val['image']
                ];
            }
          
            return success('商品排序成功', $arr);
        } catch (\Exception $exception) {

            return fails($exception->getMessage(), '');
        }


    }


   /**
     * 分类列表
     * @return mixed
     */
    public function index(Request $request)
    {

        $keyword = $request->input('0');

        $where = [];
        if (!empty($keyword)) {
            $where[] = ['cate_name', 'like', "$keyword%"];
        }
        try {
            $result = $this->product->index($where)->toArray();
            $res = new Encryption();
            $data = $res->subTree($result);
            return success('数据查询成功', $data);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }


    }


    /**
     * 商品分类添加
     * @param StoreCategoryRequest $request
     * @return mixed
     */
    public function save(StoreCategoryRequest $request)
    {

        $postData = $request->post();
        $audit = new Audit();
        $text = $audit->text($postData['cate_name']);
        if ($text['conclusion'] == "不合规") {
            return fails($text, '');
        }
        try {
            $result = $this->product->save($postData);
            return success('数据添加成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }


    }

     /**
     * 分类图标上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function upload(Request $request)
    {
        $file = $request->file();
        $file = $file['file'];
        $audit = new Audit();
        $res = $audit->image($file);
        if ($res['conclusion'] == '不合规') {
            return fails($res, '');
        }
        try {
            $img = new Image();
            $result = $img->uploadFile($file, 'zn7KQEaKC-dP0jwmUc4alsvnDHzR_Ys1NbsifCGO', 'S9Y07b6IqPaLQGcVY0fLR7Nc_m_9I_a3FSlA3KuR', 'wyt-vue', 'http://imgvue.wyt.plus/');
            return $this->success('图片上传成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }

    }
/**
     * 修改之前查询
     * @param $id
     * @return array
     */
    public function edit($id)
    {
        //参数判断
        if (!is_numeric($id) || $id == '') {
            return fails('参数格式不正确', '');
        }
        try {
            $result = $this->product->edit($id);
            return success('查询成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }

    }


   /**
     * 分类列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function categoryList()
    {
        try {
            $result = $this->product->categoryList()->toArray();
            $res = new Encryption();
            $data = $res->treeLevel($result);
            return $this->success('查询成功', $data);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }

    }

     /**
     * 分类修改
     * @param StoreCategoryRequest $request
     * @param $id
     * @return array
     */
    public function updateCategory(StoreCategoryRequest $request, $id)
    {
        //参数校验
        if (!is_numeric($id) || $id == '') {
            return fails('参数格式不正确', '');
        }

        $postData = $request->except('sub');
        try {
            $result = $this->product->update($id, $postData);
            return success('修改成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }

    }

    /**
     * 商品分类删除
     * @param $id
     * @return array|void
     */
    public function delete($id)
    {

        //参数校验
        if (!is_numeric($id) || $id == '') {
            return fails('参数格式不正确', '');
        }
        $result = $this->product->read($id)->toArray();
      
        if ($result['is_show'] == 1) {
            return fails('该商品上架中不可删除', '');
        }
        try {
            $res = $this->product->delete($id);
            return success('删除成功', $res);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(),'');
        }

    }
 /**
     * 商品分类状态修改
     * @param $id
     * @return array
     */
    public function change($id)
    {

        //参数校验
        if (!is_numeric($id) || $id == '') {
            return fails('参数格式不正确', '');
        }
        try {
            $result = $this->product->change($id);
            return success('状态修改成功', $result);
        } catch (\Exception $exception) {
            return fails($exception->getMessage(), '');
        }


    }


    /**
     * @param Request $request
     * 规格添加
     */
    public function AddMoreSpec(Request $request)
    {

        $param=$request->input();
         $res=$this->product->AddMoreSpec($param);

    }

    public function AttrRuleList()
    {
        $res=$this->product->AttrRuleList();

        return ['code'=>20000,'message'=>'success','data'=>$res];
    }

    public function AttrDel(Request $request)
    {
        $id=$request->input('id');
        $res=$this->product->AttrDel($id);
        if($res)
        {
            return success('删除成功','');
        }
        else
        {
            return fails('删除失败','');
        }

    }

    /**
     * @param Request $request
     * 商品浏览量排行榜 cqf
     */
    public function productRankList(Request $request){
        return $this->product->productRankList();
    }
}
